﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SimhoppKjellsFavoriterVT13
{
    public class PresenterMain
    {
        #region Properties
        public IMain view { get; set; }
        public Contest contest { get; set; }
        #endregion

        public PresenterMain(IMain view, Contest cr)
        {
            //string filepath;
            this.contest = cr;
            this.view = view;
            view.EventDeleteContestant += DeleteContestant;
            view.EventDeleteJudge += DeleteJudge;
            view.EventNewContestant += NewContestant;
            view.EventNewJudge += NewJudge;

            view.EventNewJump += NewJump;
            view.EventEditJump += EditJump;
            view.EventDeleteJump += DeleteJump;

            view.EventReadFromFile += ReadFromFile;
            view.EventSaveToFile += SaveToFile;
             
            view.EventEditContestant += EditContestant;
            view.EventEditJudge += EditJudge;
            view.EventPrintContestant += printContestants;
            view.EventPrintJudges += printJudges;
            view.EventPrintResult += printResult;

        }

        public void DeleteContestant(int ID)
        {
            contest.removeContestant(ID);
        }

        public void DeleteJudge(int ID)
        {
            contest.removeJudge(ID);
        }

        public void DeleteJump(int contestantID, int jump)
        {
            ((Contestant)contest.getPersonByID(contestantID)).removeJump(jump);
        }

        public void NewContestant(String name, DateTime birthdate, Gender g, String nationality, int ID)
        {
            contest.addContestant(name, birthdate, g, nationality, ID);
        }

        public void NewJudge(String name, DateTime birthdate, Gender g, String nationality, int ID)
        {
            contest.addJudge(name, birthdate,g, nationality, ID);
        }

        public void NewJump(int contestantID, float jumpDif, string jumpID, List<Judge> judges)
        {
            Jump j = new Jump();
            j.jumpID = jumpID;
            j.difficulty = jumpDif;
            Contestant c = (Contestant)contest.getPersonByID(contestantID);
            c.addJump(j);

            List<Judge> templist = new List<Judge>();

            for (int i = 0; i < judges.Count; i++)
            {
                j.addJudge( judges[i] );
            }
        }

        public void EditJudge(String name, DateTime birthdate, Gender g, String nationality, int ID)
        {
            Person p = contest.getPersonByID(ID);
            p.name = name;
            p.birthDate = birthdate;
            p.nationality = nationality;
        }
        public void EditContestant(String name, DateTime birthdate, Gender g, String nationality, int ID)
        {
            Person p = contest.getPersonByID(ID);
            p.name = name;
            p.birthDate = birthdate;
            p.nationality = nationality;
        }

        public void EditJump(int contestantID, int jump, List<float> judgeValues)
        {
            foreach (Contestant c in contest.Contestants)
            {
                if (c.ID == contestantID)
                {
                    Jump j = c.getJump(jump);
                    for (int i = 0; i < j.Judges.Count; i++)
                    {
                        j.getJudge(i + 1).Approach = judgeValues[i * 4];
                        j.getJudge(i + 1).Entry = judgeValues[i * 4 + 1];
                        j.getJudge(i + 1).Flight = judgeValues[i * 4 + 2];
                        j.getJudge(i + 1).Takeoff = judgeValues[i * 4 + 3];
                    }
                    return;
                }
            }
        }

        public void ReadFromFile(string filePath)
        {            
            System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(contest.GetType());

            //Läs XML-filen
            System.IO.StreamReader file = new System.IO.StreamReader(filePath);

            //Deserialize the XML-file
            contest = (Contest)reader.Deserialize(file);
                       
        }

        public void SaveToFile(string filePath)
        {
            System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(contest.GetType());
            System.IO.StreamWriter file = new System.IO.StreamWriter(filePath);
            writer.Serialize(file, contest);
            file.Close();
        }

        public void printDocument()
        {

        }

        public void printContestants()
        {
            PrintContestants printContestants = new PrintContestants(contest.Contestants);
            if (contest.Contestants.Count > 0)
            {
                printContestants.printingContestants();
            }

            else
            {
                System.Windows.Forms.MessageBox.Show("No contestants to print.");
            }
        }

        public void printJudges()
        {
            PrintJudges printJudge = new PrintJudges(contest.Judges);

            if (contest.Judges.Count > 0)
            {
                printJudge.printingJudges();
            }

            else
            {
                System.Windows.Forms.MessageBox.Show("No judges in the contest.");
            }
        }

        public void printResult()
        {
            PrintResult printResult = new PrintResult(contest.Contestants);

            if (contest.Contestants.Count > 0)
            {
                printResult.printingResult();
            }

            else
            {
                System.Windows.Forms.MessageBox.Show("No result to print.");
                //Ge Felmeddelande 
            }
        }


    }
}
